<template>
  <div class="offline">
    <BaseSearch :config="searchConfig" v-bind="form" ref="search"></BaseSearch>
    <BaseTable :columns="columns" v-bind="table"></BaseTable>
    <OfflinePop ref="offlinePop" @closePop="addEditSuccess"></OfflinePop>
  </div>
</template>

<script>
import BaseSearch from '@/components/BaseSearch/index.vue'
import BaseTable from '@/components/BaseTable/index.vue'
import { FormTableMixin } from '@/mixins/form-table'
import http from '@/utils/request'
import { CREDITEXTENSION_OFFLINE_STATUS } from '@/constants/auditModel'
import OfflinePop from '../components/OfflinePop.vue'

const service = async params => {
  const { records, total } = await http.get('/credit_applys', {
    params: { ...params, applyType: 3, listType: 1 },
  })

  return {
    data: records,
    total: total,
  }
}

export default {
  name: 'offline',
  components: { BaseSearch, BaseTable, OfflinePop },
  mixins: [FormTableMixin(service)],
  data() {
    const isCanSign = this.$checkAccess('MENU_OFFLINE_SIGNING')
    const isCanDetail = this.$checkAccess('MENU_OFFLINE_DETAIL')
    return {
      searchConfig: [
        { component: 'input', prop: 'searchStr', label: '搜索', placeholder: '企业名称/统一信用代码' },
        { component: 'select', prop: 'appCode', label: '来源', options: [] },
      ],
      columns: [
        { label: '企业名称', prop: 'tenantName' },
        { label: '统一信用代码', prop: 'uscCode' },
        { label: '产品名称', prop: 'productName' },
        { label: '受理日期', prop: 'createDate' },
        {
          label: '审核状态',
          prop: 'status',
          render: (value, row) => {
            return <span>{CREDITEXTENSION_OFFLINE_STATUS[row.status]}</span>
          },
        },
        { label: '来源', prop: 'appName' },
        {
          label: '操作',
          render: (value, row) => {
            return (
              <div>
                {isCanSign && (
                  <el-button
                    type="text"
                    onClick={() => {
                      this.openPop(row)
                    }}>
                    {row.status === '3' ? '签约' : '签约信息'}
                  </el-button>
                )}
                {isCanDetail && (
                  <el-button
                    type="text"
                    onClick={() => {
                      this.goToDetail(row)
                    }}>
                    详情
                  </el-button>
                )}
              </div>
            )
          },
        },
      ],
    }
  },
  methods: {
    openPop(currentRow) {
      if (currentRow.status === '3') {
        this.$refs.offlinePop.show({
          id: currentRow.id,
          tenantName: currentRow.tenantName,
          discount: currentRow.discount,
          title: '确认签约',
          btnText: '确认',
          isRequestDetailInfo: false,
        })
      } else {
        this.$refs.offlinePop.show({
          id: currentRow.id,
          tenantName: currentRow.tenantName,
          accountName: currentRow.accountName,
          accountNumber: currentRow.accountNumber,
          discount: currentRow.discount,
          title: '签约信息',
          btnText: '返回',
          isRequestDetailInfo: false,
        })
      }
    },
    goToDetail(currentRow) {
      this.$router.push({
        name: 'offlineDetail',
        query: {
          applyId: currentRow.id,
          appCode: currentRow.appCode,
        },
      })
    },
    //获取来源列表
    async getSourceListRequest() {
      const sourceList = await this.$http.get(`/app/list`)
      const list = sourceList.map(item => {
        return {
          label: item.name,
          value: item.code,
        }
      })
      this.searchConfig[1].options = [...list]
    },
    addEditSuccess(isClose) {
      if (isClose) {
        this.$refs.search.$refs.searchForm.resetFields()
        this.form.onSubmit()
      }
    },
  },
  mounted() {
    this.getSourceListRequest()
  },
}
</script>

<style lang="scss" scoped></style>
